package com.dhy.controller;

import com.dhy.pojo.Department;
import com.dhy.pojo.Employee;
import com.dhy.query.BaseQuery;
import com.dhy.service.IBaseService;
import com.dhy.service.IEmployeeService;
import com.dhy.utils.AjaxResult;
import com.dhy.utils.annotation.MyPermissionAnnotation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("employees")
@CrossOrigin
@MyPermissionAnnotation(name = "员工管理")
@Api(value = "部门的API", description = "部门相关的CRUD功能")
public class EmployeeController {

    @Autowired
    IEmployeeService iEmployeeService;

    @GetMapping("/selectPageSize")
    public AjaxResult selectPageSize() {
        System.out.println("开始方法");
        AjaxResult ajaxResult = AjaxResult.ajaxResultBuild();
        try {
            Long pageSize = iEmployeeService.selectPageSize();
            return ajaxResult.setMessage("操作成功").setSuccess(true).setData(pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            return ajaxResult.setSuccess(false).setMessage("操作失败");
        }
    }

    @PostMapping("/limitSelectAll")
    public AjaxResult selectLimit(@RequestBody BaseQuery BaseQuery) {
        AjaxResult ajaxResult = AjaxResult.ajaxResultBuild();
        try {
            List<Employee> employees = iEmployeeService.selectLimit(BaseQuery);
            return ajaxResult.setMessage("操作成功").setSuccess(true).setData(employees);
        } catch (Exception e) {
            e.printStackTrace();
            return ajaxResult.setSuccess(false).setMessage("操作失败");
        }
    }

    //添加和修改共用一个接口
    @PostMapping
    public AjaxResult insertOrUpdate(@RequestBody Employee employee) {
        AjaxResult ajaxResult = AjaxResult.ajaxResultBuild();
        try {
            if (employee.getId() == null) {
                //执行修改方法
                iEmployeeService.insert(employee);
                ajaxResult.setMessage("操作成功").setSuccess(true);
            } else {
                //执行增加方法
                iEmployeeService.update(employee);
                ajaxResult.setMessage("操作成功").setSuccess(true);
            }
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setMessage("操作失败").setSuccess(false);
        }
        return ajaxResult;
    }

    //删除接口
    @RequestMapping(path = "/{id}", method = RequestMethod.DELETE)
    public AjaxResult deleteById(@PathVariable("id") Long id) {
        AjaxResult ajaxResult = AjaxResult.ajaxResultBuild();
        try {
            iEmployeeService.deleteById(id);
            ajaxResult.setMessage("操作成功").setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setMessage("操作失败").setSuccess(false);
        }
        return ajaxResult;
    }

    //id查询接口
    /*
     * 传递单个参数的方法
     * */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    public AjaxResult selectById(@ApiParam(name = "编号", required = true) @PathVariable("id") Long id) {
        AjaxResult ajaxResult = AjaxResult.ajaxResultBuild();
        try {
            Employee employee = iEmployeeService.selectById(id);
            if (employee == null) {
                ajaxResult.setMessage("操作失败").setSuccess(false);
            } else {
                ajaxResult.setMessage("操作成功").setSuccess(true).setData(employee);
                System.out.println();
            }

        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setMessage("操作失败").setSuccess(false);
        }
        return ajaxResult;
    }

    //name查询接口
    /*
     * 区分同种请求方法：增加小路径
     * */
    @GetMapping("/username/{username}")
    public AjaxResult selectByName(@PathVariable("username") String username) {
        AjaxResult ajaxResult = AjaxResult.ajaxResultBuild();
        return ajaxResult;
    }

    //id，name共同查询接口
    /*
     * 传递多个参数的方法
     * */
    @GetMapping("/id/{id}/username/{username}")
    public AjaxResult selectByNameAndId(@PathVariable("id") Long id, @PathVariable("username") String username) {
        AjaxResult ajaxResult = AjaxResult.ajaxResultBuild();
        return ajaxResult;
    }

    @GetMapping
    public AjaxResult selectAll() {
        AjaxResult ajaxResult = AjaxResult.ajaxResultBuild();
        try {
            List<Employee> employees = iEmployeeService.selectAll();
            return ajaxResult.setMessage("操作成功").setSuccess(true).setData(employees);
        } catch (Exception e) {
            e.printStackTrace();
            return ajaxResult.setSuccess(false).setMessage("操作失败");
        }
    }

    @GetMapping("/department/{id}")
    public AjaxResult selectByWithDepartment(@PathVariable("id") Long id) {
        AjaxResult ajaxResult = AjaxResult.ajaxResultBuild();
        try {
            Employee employee = iEmployeeService.selectByWithDepartment(id);
            ajaxResult.setMessage("操作成功").setSuccess(true).setData(employee);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setMessage("操作失败").setSuccess(false);
        }
        return ajaxResult;
    }
}
